{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "自定义循环.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "machine_shape": "hm",
      "authorship_tag": "ABX9TyNzbc5VqhnKRSxd6nl9+93M",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/sunyingjian/study-tensorflow/blob/master/%E8%87%AA%E5%AE%9A%E4%B9%89%E5%BE%AA%E7%8E%AF.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pW-pozZ_Jd1_",
        "colab_type": "code",
        "outputId": "e31baecb-9a15-4663-963c-0eb1f3e23cfc",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "#标准开场,引入tf2.0\n",
        "%tensorflow_version 2.x\n",
        "import tensorflow as tf\n",
        "print('tensorflow:',format(tf.__version__))\n",
        "import numpy as np\n",
        "from tensorflow import keras\n",
        "import glob\n",
        "from tensorflow.keras import layers\n",
        "import matplotlib.pyplot as plt\n",
        "%matplotlib inline"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensorflow: 2.2.0-rc1\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KFGUtVggHVQ1",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7ej_tgvzeGiS",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#引入minist数据集\n",
        "(train_image,train_label),(test_image,test_label)=tf.keras.datasets.mnist.load_data()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UeooLI63eah1",
        "colab_type": "code",
        "outputId": "c276257d-424f-49fb-eaba-5a5a193f4961",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "#给图像增加维度\n",
        "train_image=tf.expand_dims(train_image,-1)\n",
        "train_image.shape"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "TensorShape([60000, 28, 28, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Ghb6l3hpfR5p",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#test图像增加维度\n",
        "test_image=tf.expand_dims(test_image,-1)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8XAo-r-FNz7t",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#预处理，改变数据类型\n",
        "train_image=tf.cast(train_image/255,tf.float32)\n",
        "test_image=tf.cast(test_image/255,tf.float32)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KnGU8AJoPL74",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#预处理，转换label的数据类型\n",
        "train_label=tf.cast(train_label,tf.int64)\n",
        "test_label=tf.cast(test_label,tf.int64)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Nn56FKlEexgv",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#把它作为tf.data加载进来,建立dataset\n",
        "dataset=tf.data.Dataset.from_tensor_slices((train_image,train_label))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0IyLgIH7P2Ro",
        "colab_type": "code",
        "outputId": "df70a54f-8bd4-446a-b5fe-9c4ffd410a3b",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "dataset"
      ],
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<TensorSliceDataset shapes: ((28, 28, 1), ()), types: (tf.float32, tf.int64)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "IWCIbKsdf9Cr",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "test_dataset=tf.data.Dataset.from_tensor_slices((test_image,test_label))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "St8fuVmyRHmF",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#对数据进行乱序，这里对10000个数据进行乱序混淆\n",
        "#批次的大小,用batch\n",
        "dataset=dataset.shuffle(10000).batch(32)\n",
        "#这里没有设置repeat\n",
        "test_dataset=test_dataset.batch(32)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pjsCuyt2R5bO",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#建立模型，进行自定义训练,把整个网络写在list里\n",
        "#这里input_shape如何让添加任意形状的图片呢，我们可以这样写参数(none,none,1)\n",
        "model=tf.keras.Sequential([tf.keras.layers.Conv2D(16,[3,3],activation='relu',input_shape=(28,28,1)),\n",
        "                           tf.keras.layers.Conv2D(32,[3,3],activation='relu'),\n",
        "                           tf.keras.layers.GlobalAveragePooling2D(),\n",
        "                           tf.keras.layers.Dense(10)\n",
        "                           ])\n",
        "#这里最后一层我没有写激活函数所以我们再后面的lossfunction里有写from_logitc=ture"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "W35_adyeO_95",
        "colab_type": "code",
        "outputId": "e0e619ce-4ee3-437b-ccfe-70c360a02b5d",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        }
      },
      "source": [
        "#看一下model的可训练参数\n",
        "model.trainable_variables"
      ],
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[<tf.Variable 'conv2d/kernel:0' shape=(3, 3, 1, 16) dtype=float32, numpy=\n",
              " array([[[[-0.07251079,  0.14896826, -0.01542766, -0.1483107 ,\n",
              "           -0.01370516,  0.10915162, -0.18296751,  0.1504984 ,\n",
              "            0.07748096, -0.00580594,  0.06531979,  0.12766509,\n",
              "            0.1606149 , -0.10996465, -0.18139958,  0.07155599]],\n",
              " \n",
              "         [[-0.04529075, -0.13903813,  0.04226151, -0.19372435,\n",
              "            0.19088109, -0.0483937 , -0.08575062, -0.12797624,\n",
              "            0.04447636, -0.00594962,  0.0272837 ,  0.09850518,\n",
              "            0.01890312,  0.19436453,  0.14159967,  0.13545142]],\n",
              " \n",
              "         [[-0.05804266,  0.00544901,  0.04821381, -0.00765342,\n",
              "           -0.14892723,  0.0902264 , -0.15722658,  0.03649324,\n",
              "           -0.18054733, -0.1702545 , -0.19263107,  0.06331636,\n",
              "            0.13785164, -0.19610728,  0.1170717 , -0.06297179]]],\n",
              " \n",
              " \n",
              "        [[[-0.09805328,  0.05294393, -0.17177144,  0.15200232,\n",
              "            0.09248696,  0.18757446, -0.1359687 , -0.05673753,\n",
              "            0.04390535,  0.15217464, -0.14602041, -0.13060656,\n",
              "           -0.10791377, -0.19339135,  0.03978787, -0.15257874]],\n",
              " \n",
              "         [[-0.07001701, -0.00093815,  0.02165622,  0.17834951,\n",
              "           -0.18491796, -0.10127191,  0.11509506,  0.12291543,\n",
              "            0.17101504,  0.12998246, -0.04829761,  0.03192987,\n",
              "            0.11150463, -0.05964534,  0.11880027,  0.02901238]],\n",
              " \n",
              "         [[ 0.0282765 ,  0.13557608, -0.13310476,  0.11119123,\n",
              "           -0.07450691, -0.07432504, -0.1409307 , -0.18503822,\n",
              "            0.02781263,  0.04349402, -0.13104023,  0.11963259,\n",
              "           -0.10840579, -0.12221548, -0.06211972, -0.08422622]]],\n",
              " \n",
              " \n",
              "        [[[-0.12425531,  0.18679689, -0.16920674,  0.17224012,\n",
              "           -0.05260409,  0.05452041,  0.00095052, -0.19646932,\n",
              "            0.1568007 , -0.02795988, -0.13178673, -0.09727624,\n",
              "           -0.08900417, -0.00942267, -0.14059614,  0.05794035]],\n",
              " \n",
              "         [[-0.07907504,  0.05118527,  0.17909668, -0.17481485,\n",
              "            0.1151555 ,  0.01527068, -0.18666303, -0.10632276,\n",
              "           -0.05467829, -0.12391815, -0.18735954, -0.1220838 ,\n",
              "           -0.18178192,  0.13361977, -0.02069263,  0.04516922]],\n",
              " \n",
              "         [[-0.09744763,  0.05775134,  0.16590033,  0.05881636,\n",
              "            0.07219483,  0.19457619, -0.01690522,  0.1278203 ,\n",
              "            0.18054311, -0.02891475, -0.1721586 , -0.07456961,\n",
              "            0.08378439, -0.11780462,  0.14070968, -0.03161094]]]],\n",
              "       dtype=float32)>,\n",
              " <tf.Variable 'conv2d/bias:0' shape=(16,) dtype=float32, numpy=\n",
              " array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
              "       dtype=float32)>,\n",
              " <tf.Variable 'conv2d_1/kernel:0' shape=(3, 3, 16, 32) dtype=float32, numpy=\n",
              " array([[[[-0.06899481, -0.02728972,  0.00452365, ..., -0.0327726 ,\n",
              "            0.08046236, -0.05194902],\n",
              "          [ 0.06868938,  0.03243872, -0.03648425, ..., -0.07615781,\n",
              "           -0.06516167,  0.00582343],\n",
              "          [-0.11258058,  0.07658776, -0.05573929, ...,  0.06649644,\n",
              "           -0.08202777,  0.08241179],\n",
              "          ...,\n",
              "          [ 0.06976146, -0.08615305,  0.10553453, ...,  0.1122554 ,\n",
              "           -0.06140692, -0.04204322],\n",
              "          [-0.11606006,  0.04243328, -0.10136061, ..., -0.10446453,\n",
              "            0.00361601,  0.01039892],\n",
              "          [-0.01252349,  0.03589071,  0.09041741, ...,  0.07689052,\n",
              "           -0.04837118, -0.03655113]],\n",
              " \n",
              "         [[-0.11446924,  0.08889083,  0.03843079, ..., -0.07338104,\n",
              "            0.11643758, -0.072056  ],\n",
              "          [-0.01711651,  0.06865772,  0.0343271 , ..., -0.0975985 ,\n",
              "           -0.02754451, -0.07108839],\n",
              "          [ 0.11017027,  0.108526  , -0.0696643 , ...,  0.00089503,\n",
              "           -0.10193431, -0.05754495],\n",
              "          ...,\n",
              "          [-0.05892944,  0.10481805, -0.03171752, ...,  0.05683663,\n",
              "            0.04428671, -0.00904581],\n",
              "          [ 0.07614747,  0.0358103 , -0.10897882, ..., -0.10012726,\n",
              "            0.02122397,  0.02653117],\n",
              "          [-0.00383196, -0.05032109,  0.03975645, ..., -0.09756029,\n",
              "            0.05932374, -0.07287791]],\n",
              " \n",
              "         [[-0.02138036, -0.00821982,  0.07913908, ..., -0.09819625,\n",
              "            0.0550754 ,  0.00977894],\n",
              "          [ 0.10481291,  0.06121548, -0.02007123, ...,  0.08993306,\n",
              "            0.08780273, -0.09878232],\n",
              "          [-0.10417762, -0.01276257, -0.0248513 , ...,  0.09801123,\n",
              "            0.01847398, -0.08944317],\n",
              "          ...,\n",
              "          [-0.00145803,  0.05670696,  0.04403838, ..., -0.08480281,\n",
              "           -0.09909932,  0.03262127],\n",
              "          [-0.03228039, -0.10127509,  0.05994745, ...,  0.01853765,\n",
              "           -0.07908395, -0.06856373],\n",
              "          [ 0.02084035,  0.02730941, -0.01138827, ...,  0.10528281,\n",
              "            0.03881112, -0.02754369]]],\n",
              " \n",
              " \n",
              "        [[[-0.04513528,  0.05369604, -0.04623262, ..., -0.10676662,\n",
              "           -0.06929785, -0.00678685],\n",
              "          [ 0.06687047, -0.01744607,  0.05680727, ..., -0.11371551,\n",
              "           -0.01671575, -0.06752934],\n",
              "          [-0.02822909, -0.10570129, -0.01116816, ...,  0.00622326,\n",
              "            0.09293868, -0.01118701],\n",
              "          ...,\n",
              "          [ 0.00925412,  0.09437654, -0.04291853, ..., -0.02705555,\n",
              "           -0.08684945,  0.08926011],\n",
              "          [-0.07130291, -0.0309955 ,  0.01642271, ..., -0.04843628,\n",
              "            0.05335063, -0.07925091],\n",
              "          [-0.02635246,  0.01261494,  0.00605482, ..., -0.01910902,\n",
              "           -0.11429093,  0.01441906]],\n",
              " \n",
              "         [[ 0.08229732,  0.03826622, -0.07573918, ...,  0.05021472,\n",
              "           -0.02587732, -0.10489575],\n",
              "          [ 0.0558399 ,  0.09404949, -0.01204582, ..., -0.05522746,\n",
              "           -0.09897491,  0.11558872],\n",
              "          [-0.03333566,  0.08185839, -0.01627711, ..., -0.03277311,\n",
              "            0.05806281, -0.06039787],\n",
              "          ...,\n",
              "          [ 0.08858886,  0.04934541, -0.00094524, ..., -0.07708097,\n",
              "            0.0386624 ,  0.02536864],\n",
              "          [ 0.02703328, -0.04539845, -0.06713004, ..., -0.06805317,\n",
              "            0.06367833, -0.05216142],\n",
              "          [-0.05952695, -0.00452452, -0.07261157, ..., -0.11219323,\n",
              "            0.10173791, -0.09761536]],\n",
              " \n",
              "         [[-0.10613358,  0.08822364,  0.09304502, ...,  0.05862644,\n",
              "            0.06100862,  0.08797138],\n",
              "          [-0.01351113,  0.0733109 , -0.02577642, ..., -0.11430708,\n",
              "            0.02998944, -0.0979257 ],\n",
              "          [ 0.11751128,  0.009671  , -0.07859078, ...,  0.10401999,\n",
              "           -0.03416868,  0.02770663],\n",
              "          ...,\n",
              "          [-0.00749253, -0.02259113,  0.07909719, ..., -0.08850712,\n",
              "           -0.01633685,  0.01102202],\n",
              "          [ 0.03316761, -0.05554252,  0.01808805, ..., -0.08537053,\n",
              "            0.02992535,  0.00096396],\n",
              "          [ 0.1175304 ,  0.09826825, -0.00200414, ..., -0.07735483,\n",
              "           -0.04576319,  0.10502627]]],\n",
              " \n",
              " \n",
              "        [[[-0.09392051, -0.07574041,  0.10139453, ..., -0.04726963,\n",
              "            0.06655813,  0.0274739 ],\n",
              "          [-0.09923529,  0.04667673, -0.0446645 , ...,  0.11199716,\n",
              "            0.05666778, -0.00750599],\n",
              "          [ 0.0013956 ,  0.03981132,  0.10609654, ..., -0.06178096,\n",
              "            0.08808009, -0.11304465],\n",
              "          ...,\n",
              "          [ 0.0089344 ,  0.0080616 ,  0.05357596, ..., -0.00138483,\n",
              "            0.05820427, -0.11127291],\n",
              "          [ 0.0294069 ,  0.08080799, -0.05492622, ...,  0.02513   ,\n",
              "            0.06609627,  0.07336336],\n",
              "          [ 0.06167165, -0.08729239,  0.06797356, ..., -0.0425877 ,\n",
              "            0.09623405, -0.10488856]],\n",
              " \n",
              "         [[ 0.11060876,  0.0504028 ,  0.02306286, ...,  0.06660492,\n",
              "           -0.10895663, -0.06118894],\n",
              "          [ 0.02772813,  0.10910711, -0.11698394, ..., -0.10219082,\n",
              "            0.0283983 ,  0.05826847],\n",
              "          [ 0.06834736,  0.07495912, -0.10109287, ..., -0.09664003,\n",
              "           -0.01579883,  0.00610739],\n",
              "          ...,\n",
              "          [-0.05129409, -0.08546341, -0.11647526, ...,  0.05211269,\n",
              "            0.05812373, -0.08916399],\n",
              "          [ 0.06172294,  0.11076166,  0.03985729, ...,  0.0521202 ,\n",
              "            0.06243328,  0.04173594],\n",
              "          [ 0.00076949, -0.09761971,  0.11388775, ...,  0.06350269,\n",
              "            0.08629294,  0.00765336]],\n",
              " \n",
              "         [[-0.108492  , -0.02293701, -0.07391399, ...,  0.10610767,\n",
              "           -0.06992207,  0.06844466],\n",
              "          [-0.03631938, -0.07373235, -0.03071519, ...,  0.10765491,\n",
              "           -0.05241203,  0.02599562],\n",
              "          [ 0.07845732,  0.08461662, -0.06482363, ..., -0.00227818,\n",
              "           -0.01499312,  0.07930148],\n",
              "          ...,\n",
              "          [-0.08920844, -0.06512554, -0.09063978, ...,  0.09147743,\n",
              "           -0.07594311, -0.05360568],\n",
              "          [ 0.0361285 , -0.05911882, -0.01894009, ..., -0.08232109,\n",
              "           -0.02464731,  0.08644687],\n",
              "          [-0.01239741,  0.09753983,  0.04406425, ...,  0.05525631,\n",
              "            0.06826393, -0.09762028]]]], dtype=float32)>,\n",
              " <tf.Variable 'conv2d_1/bias:0' shape=(32,) dtype=float32, numpy=\n",
              " array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
              "        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
              "       dtype=float32)>,\n",
              " <tf.Variable 'dense/kernel:0' shape=(32, 10) dtype=float32, numpy=\n",
              " array([[ 0.2003155 , -0.02550885,  0.1946167 , -0.0623771 , -0.1588016 ,\n",
              "          0.18629584,  0.00562671, -0.00633147, -0.15920801, -0.1574116 ],\n",
              "        [ 0.16816786, -0.23539495, -0.18937504,  0.2669219 , -0.2635163 ,\n",
              "          0.06661713, -0.28132945,  0.36552545,  0.27338222,  0.08687055],\n",
              "        [-0.1926788 , -0.1833403 , -0.26572815,  0.2158874 ,  0.0768829 ,\n",
              "          0.15746728,  0.22584948,  0.36994234, -0.26597238,  0.3067343 ],\n",
              "        [ 0.3634319 ,  0.0588614 ,  0.28280833, -0.28192186, -0.30647802,\n",
              "          0.09031552, -0.19955997, -0.36793822, -0.3231035 ,  0.2656398 ],\n",
              "        [ 0.1855661 , -0.36302152, -0.16150548, -0.36144617, -0.2698879 ,\n",
              "          0.20999822,  0.1447325 , -0.28304523, -0.06199041,  0.09341922],\n",
              "        [ 0.31376114,  0.33439896,  0.36282203,  0.36834714, -0.18986121,\n",
              "         -0.0008733 , -0.32429135, -0.19178559,  0.19127944,  0.28643927],\n",
              "        [ 0.07443622, -0.04224488, -0.19034322,  0.12723866, -0.1402991 ,\n",
              "         -0.12786007,  0.08045292, -0.10308453,  0.3507264 ,  0.3088763 ],\n",
              "        [ 0.13954905, -0.29397297, -0.2623451 ,  0.26990387,  0.2728164 ,\n",
              "          0.3712118 ,  0.23713478, -0.23137958,  0.33197114, -0.2494245 ],\n",
              "        [-0.2716418 ,  0.29294178, -0.35553786,  0.16870698, -0.23043177,\n",
              "         -0.05657512, -0.36358953, -0.24608245, -0.3424275 , -0.2816354 ],\n",
              "        [-0.27504754, -0.34587604, -0.28437737,  0.2806081 , -0.0516814 ,\n",
              "          0.28540668, -0.13836363, -0.36756858, -0.09078807, -0.0712806 ],\n",
              "        [ 0.00142235,  0.01157674,  0.2021881 , -0.34585217, -0.17281033,\n",
              "          0.32292894,  0.12482026,  0.04503992,  0.36860117,  0.17886356],\n",
              "        [-0.12805416,  0.3696814 , -0.21936949,  0.29075173, -0.3492062 ,\n",
              "          0.30589733, -0.36695212, -0.15928128,  0.05820096, -0.10390368],\n",
              "        [-0.10606912, -0.04117811, -0.30096787, -0.06845751,  0.3520309 ,\n",
              "          0.06250712,  0.18102828, -0.09898329, -0.01089782, -0.13472816],\n",
              "        [-0.18902215,  0.2508593 ,  0.14882675, -0.05597144,  0.08340991,\n",
              "          0.23673287,  0.1761274 , -0.27322894,  0.16222855,  0.21588895],\n",
              "        [ 0.17756203,  0.03230506, -0.25383142, -0.28385437,  0.2843077 ,\n",
              "         -0.01984584, -0.17785427, -0.26628172, -0.00937229,  0.05585837],\n",
              "        [ 0.32585707, -0.25534624, -0.10155261, -0.22238353, -0.2853105 ,\n",
              "         -0.2142176 , -0.28668076, -0.13156761,  0.18573913, -0.24394819],\n",
              "        [-0.17355944, -0.3181683 , -0.12151957, -0.18998493,  0.24333599,\n",
              "         -0.17979811, -0.34738815, -0.32620007, -0.30267903,  0.18011883],\n",
              "        [-0.35655227, -0.00129062, -0.373043  ,  0.23189476, -0.00581872,\n",
              "         -0.2577381 , -0.15679225,  0.15943864,  0.27705196, -0.25574183],\n",
              "        [-0.07381874, -0.08724084,  0.34018627, -0.26248217, -0.33550295,\n",
              "         -0.31956047, -0.05863178,  0.3408951 , -0.11324227, -0.26627722],\n",
              "        [-0.16748056,  0.32875922,  0.31270996, -0.32622972,  0.30456015,\n",
              "          0.00510395, -0.11303291, -0.23721734, -0.08101451,  0.05592945],\n",
              "        [ 0.2294006 ,  0.05285317, -0.3375214 ,  0.25330344, -0.28703645,\n",
              "          0.27373162, -0.23282771, -0.10650256,  0.20006534, -0.25727233],\n",
              "        [-0.24132499, -0.24702404, -0.0090076 ,  0.11435884,  0.15614524,\n",
              "          0.04819733,  0.31599495,  0.25693044, -0.00943896, -0.05561507],\n",
              "        [-0.04208311,  0.0532738 , -0.30614507,  0.3030018 ,  0.15385601,\n",
              "         -0.11020586,  0.30022976,  0.06670147,  0.02868068,  0.32496783],\n",
              "        [-0.3502826 , -0.2042193 , -0.22174218,  0.34537575,  0.35458925,\n",
              "         -0.08864671,  0.19307753, -0.02778557, -0.16925408,  0.1520808 ],\n",
              "        [ 0.11664161,  0.232586  ,  0.15218887,  0.28696004,  0.3736209 ,\n",
              "          0.04666334,  0.05231264, -0.26284063, -0.27217695,  0.10696998],\n",
              "        [ 0.0788717 , -0.23655958, -0.01873863,  0.22560808, -0.05781689,\n",
              "          0.17269382, -0.29506683,  0.14582416, -0.36481723,  0.37499484],\n",
              "        [ 0.16020134, -0.12604535, -0.2212808 ,  0.3454114 , -0.30135238,\n",
              "         -0.27844086,  0.24097452, -0.31551537, -0.03350502,  0.04211348],\n",
              "        [ 0.01318687, -0.24473047, -0.24773875, -0.14357364, -0.3640007 ,\n",
              "         -0.06550169,  0.21624634, -0.23201029,  0.35640284,  0.1668813 ],\n",
              "        [-0.0682317 , -0.06138673,  0.21661112,  0.01040372,  0.15522894,\n",
              "          0.08681208, -0.07387742,  0.16208813,  0.29707685,  0.2306504 ],\n",
              "        [ 0.01305443, -0.23118106,  0.1629335 , -0.3302285 ,  0.35681114,\n",
              "          0.29071257,  0.35618892,  0.17127165,  0.35608307, -0.06954402],\n",
              "        [ 0.2829211 , -0.05216837,  0.19196823,  0.03265667,  0.35462698,\n",
              "          0.17217812, -0.06225049, -0.15144265, -0.302744  , -0.2123773 ],\n",
              "        [-0.19123363, -0.03711677, -0.16465665,  0.09659329, -0.08334845,\n",
              "          0.37586275, -0.20532112, -0.1332822 , -0.076653  , -0.30629563]],\n",
              "       dtype=float32)>,\n",
              " <tf.Variable 'dense/bias:0' shape=(10,) dtype=float32, numpy=array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)>]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6NRDszrB5bhN",
        "colab_type": "text"
      },
      "source": [
        "优化器"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xloAhGSLY7JO",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "optimizer=tf.keras.optimizers.Adam()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7--DhZIb5eCf",
        "colab_type": "text"
      },
      "source": [
        "自定义 损失函数"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0zxKtDFe5Y-h",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "loss_func=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Sqx7K8gZ9kBo",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#设计一个生成器\n",
        "features,labels=next(iter(dataset))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0SBhv8M2IvVI",
        "colab_type": "code",
        "outputId": "d9848c2a-a0b3-4e7b-8b4f-6909a43f3372",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "features.shape"
      ],
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "TensorShape([32, 28, 28, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "M5UNNDVGI8nM",
        "colab_type": "code",
        "outputId": "5c51874b-3a94-431f-f951-b4daa28b3cea",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "predictions=model(features)\n",
        "predictions.shape"
      ],
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "TensorShape([32, 10])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZBm7mnt1JQGb",
        "colab_type": "code",
        "outputId": "baad443d-dc04-434e-d2b2-490f3b70cc4a",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 69
        }
      },
      "source": [
        "#哪个最大呢？\n",
        "tf.argmax(predictions,axis=1)"
      ],
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(32,), dtype=int64, numpy=\n",
              "array([3, 3, 5, 3, 3, 3, 3, 3, 5, 5, 3, 5, 3, 3, 3, 5, 3, 3, 3, 5, 3, 3,\n",
              "       3, 5, 3, 5, 3, 3, 3, 3, 3, 3])>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "X9iSaXeEJdLk",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#开始训练\n",
        "#定义一个loss函数\n",
        "def loss(model,x,y):#x是实际的数据，y是实际的label\n",
        "  y_=model(x)#y_是预测的label\n",
        "  return loss_func(y,y_)\n",
        "#这样的话 我们输入一个model和数据还有标签就会返回一个loss损失函数"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "PHGoeq2LWwhM",
        "colab_type": "text"
      },
      "source": [
        "##整合metrcis到自定义循环中"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "n7tPZj1RWrRT",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "train_loss=tf.keras.metrics.Mean('train_loss')\n",
        "#所以mean方法就是在求均值\n",
        "train_accuracy=tf.keras.metrics.SparseCategoricalAccuracy('train_accuracy')\n",
        "#SparseCategoricalAcuracy就是在求均值\n",
        "test_loss=tf.keras.metrics.Mean('test_loss')\n",
        "test_accuracy=tf.keras.metrics.SparseCategoricalAccuracy('test_accuracy')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "RVp2X3YXL7BL",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#一步优化过程\n",
        "def train_step(model,images,labels):\n",
        "  with tf.GradientTape() as t:\n",
        "    pred=model(images)#这里生成预测值\n",
        "    loss_step=loss_func(labels,pred)\n",
        "    #计算损失函数\n",
        "    loss_step=loss(model,images,labels)\n",
        "  grads=t.gradient(loss_step,model.trainable_variables)\n",
        "  #优化grads 使梯度下降最快\n",
        "  optimizer.apply_gradients(zip(grads,model.trainable_variables))\n",
        "  train_loss(loss_step)\n",
        "  train_accuracy(labels,pred)\n",
        "  #这里就是应用这个梯度对可训练参数进行优化\n",
        "  #以上就完成了对一个batch数据的优化"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wErxOgHagnVO",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#建立test_step\n",
        "def test_step(model,images,labels):\n",
        "  pred=model(images)\n",
        "  loss_step=loss_func(labels,pred)\n",
        "  test_loss(loss_step)\n",
        "  test_accuracy(labels,pred)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0bZUVjaYNu32",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def train():#定义多层训练函数\n",
        "  for epoch in range(10):\n",
        "    #然后我们就可以对dataset进行迭代\n",
        "    for (batch,(image,label)) in enumerate(dataset):\n",
        "      #这里使用了enumerate枚举的方法\n",
        "      train_step(model,image,label)\n",
        "      #这样的话就进行了一次训练\n",
        "    print('Epoch{} is finshed,loss is{},acc is {}'.format(epoch,\n",
        "                                                          train_loss.result(),\n",
        "                                                          train_accuracy.result()))\n",
        "    for (batch,(image,label)) in enumerate(test_dataset):\n",
        "      #这里使用了enumerate枚举的方法\n",
        "      test_step(model,image,label)\n",
        "    print('Epoch{} is finshed,test_loss is{},test_acc is {}'.format(epoch,\n",
        "                                                          test_loss.result(),\n",
        "                                                          test_accuracy.result()))\n",
        "    #实现状态的重置，因为每个epoch是不一样的\n",
        "    test_loss.reset_states()\n",
        "    test_accuracy.reset_states()\n",
        "    train_loss.reset_states()\n",
        "    train_accuracy.reset_states()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WctYNfiySC8_",
        "colab_type": "code",
        "outputId": "bb8c4e7f-bd46-4698-ce89-8fd4f18db69f",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 363
        }
      },
      "source": [
        "train()"
      ],
      "execution_count": 24,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Epoch0 is finshed,loss is1.7392185926437378,acc is 0.34751665592193604\n",
            "Epoch0 is finshed,test_loss is1.467941403388977,test_acc is 0.447299987077713\n",
            "Epoch1 is finshed,loss is1.3740217685699463,acc is 0.5266333222389221\n",
            "Epoch1 is finshed,test_loss is1.1911839246749878,test_acc is 0.6086000204086304\n",
            "Epoch2 is finshed,loss is1.1189426183700562,acc is 0.6377833485603333\n",
            "Epoch2 is finshed,test_loss is0.9631350636482239,test_acc is 0.7053999900817871\n",
            "Epoch3 is finshed,loss is0.9279780983924866,acc is 0.7087000012397766\n",
            "Epoch3 is finshed,test_loss is0.8045744299888611,test_acc is 0.7512000203132629\n",
            "Epoch4 is finshed,loss is0.7997111082077026,acc is 0.7519333362579346\n",
            "Epoch4 is finshed,test_loss is0.708358883857727,test_acc is 0.7820000052452087\n",
            "Epoch5 is finshed,loss is0.7106226682662964,acc is 0.7828333377838135\n",
            "Epoch5 is finshed,test_loss is0.6374936699867249,test_acc is 0.8072999715805054\n",
            "Epoch6 is finshed,loss is0.6455931067466736,acc is 0.8026000261306763\n",
            "Epoch6 is finshed,test_loss is0.5955544114112854,test_acc is 0.810699999332428\n",
            "Epoch7 is finshed,loss is0.5972709655761719,acc is 0.8188333511352539\n",
            "Epoch7 is finshed,test_loss is0.5360214114189148,test_acc is 0.8388000130653381\n",
            "Epoch8 is finshed,loss is0.5557729601860046,acc is 0.8308833241462708\n",
            "Epoch8 is finshed,test_loss is0.5198466777801514,test_acc is 0.840399980545044\n",
            "Epoch9 is finshed,loss is0.5233844518661499,acc is 0.8410999774932861\n",
            "Epoch9 is finshed,test_loss is0.46450331807136536,test_acc is 0.8614000082015991\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2fxPqAzC88Kn",
        "colab_type": "text"
      },
      "source": [
        "#tf.keras.metics 汇总计算模块"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "I3ppBXcA86-S",
        "colab_type": "code",
        "outputId": "ed5aee47-9ff8-43fd-d6dc-b0833fb817ce",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "#初始化一个对象\n",
        "m=tf.keras.metrics.Mean('acc')#mean方法就是一个计算均值的方法\n",
        "#调用他\n",
        "m(10)"
      ],
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(), dtype=float32, numpy=10.0>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 25
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6IyRSOjD4AsG",
        "colab_type": "code",
        "outputId": "83591d13-d3fa-4c24-9d40-7b26ecf904d4",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "m(155)"
      ],
      "execution_count": 26,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(), dtype=float32, numpy=82.5>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 26
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eATZxuSp4G0X",
        "colab_type": "text"
      },
      "source": [
        "所以这里为什么是82.5是因为他计算了155和10的均值"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ORvA1Zkq3y6K",
        "colab_type": "code",
        "outputId": "901c2cd9-fc09-4040-c309-41414a114a0d",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "#返回他计算均值的结果\n",
        "m.result()"
      ],
      "execution_count": 27,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(), dtype=float32, numpy=82.5>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 27
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QGrkfXB74yO7",
        "colab_type": "code",
        "outputId": "1aa1227d-ccc3-4677-e8c2-d8a5fd0a13cd",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "m([30,40])\n",
        "m.result()"
      ],
      "execution_count": 28,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(), dtype=float32, numpy=58.75>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 28
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "q66l3eYT5C4a",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#重置状态\n",
        "m.reset_states()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dExpq6Sx5PtD",
        "colab_type": "code",
        "outputId": "9fd04f12-f556-44e3-d525-5aedab5c2ff5",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "m.result().numpy()"
      ],
      "execution_count": 30,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0.0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 30
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "U9UG8JSUPRc4",
        "colab_type": "text"
      },
      "source": [
        "##汇总计算模块"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-kdz9NNgPU0p",
        "colab_type": "code",
        "outputId": "3d76a48c-8d53-4583-cfd3-ce4e42c29321",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "#初始化一个对象\n",
        "a=tf.keras.metrics.SparseCategoricalAccuracy('acc')\n",
        "#为什么用这个SCA是因为我们前面的label是普通的编码不是独热编码\n",
        "a(labels,model(features))\n",
        "#这里的话就是在计算acc前面的是真的label的值，后面是预测的所以这样\n",
        "#就能求出准确率，这样我们就可以添加到我们的自定义循环的代码中，这样就很方便了"
      ],
      "execution_count": 31,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<tf.Tensor: shape=(), dtype=float32, numpy=0.9375>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 31
        }
      ]
    }
  ]
}